package com.tarena.lbs.content.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.po.ArticleCategoryPO;
import com.tarena.lbs.content.pojo.query.ArticleCategoryQuery;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    //默认分页参数
    private  static  final int DEFAULT_PAGE_SIZE = 10;
    private  static  final  int DEFAULT_PAGE_NUM = 1;

    @Autowired
    private CategoryRepository categoryRepository;

    @Override
    public PageResult<ArticleCategoryVO> pageList(ArticleCategoryQuery query) {
        return null;
    }

    @Override
    public PageResult<ArticleCategoryVO>pageResult(ArticleCategoryQuery query){
        PageResult<ArticleCategoryVO>voPage=new PageResult<>();
        log.debug(query.toString());

        PageInfo<ArticleCategoryPO>pageInfo=categoryRepository.pagesList(query);

        voPage.setPageNo(pageInfo.getPageNum());
        voPage.setPageSize(pageInfo.getPageSize());
        voPage.setTotal(pageInfo.getTotal());

        List<ArticleCategoryVO>vos =null;
        List<ArticleCategoryPO>pos=pageInfo.getList();

        if(CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                ArticleCategoryVO vo=new ArticleCategoryVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }
    @Override
    public void addCategory(ArticleCategoryParam param){
        ArticleCategoryPO po=new ArticleCategoryPO();
        BeanUtils.copyProperties(param,po);
        po.setCreateTime(new Date());
        categoryRepository.addCategory(po);

    }
    @Override
    public void deleteCategory(Integer id){
        categoryRepository.deleteCategory(id);

    }

    @Override
    public void updateCategory(ArticleCategoryParam param){
        categoryRepository.updateCategory(param);
    }

}
